package com.example.product;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.productmodule.R;

import java.util.List;

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder> {

    private List<Data> mDataList;

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView DataIv;
        TextView DataTv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            DataIv = itemView.findViewById(R.id.recycle_view_iv);
            DataTv = itemView.findViewById(R.id.recycle_view_tv);
        }
    }
    
    public RecycleViewAdapter(List<Data> mDataList) {
        this.mDataList = mDataList;
    }

    
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        final Data data = mDataList.get(position);
        holder.DataIv.setImageResource(data.getImageId());
        holder.DataTv.setText(data.getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onGoodsSlotClick(position, data);
            }
        });
    }

    @Override
    public int getItemCount() {
        if(mDataList != null){
        	return mDataList.size();
    	}
    	return 0;
    }

    public OnItemClickListenerr onItemClickListener;

    public void setOnItemClickListenerr(OnItemClickListenerr onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    public interface OnItemClickListenerr {
        void onGoodsSlotClick(int index, Data data);
    }
}
